/**
 * @Description: 初始化指令分配
 * @author Beon
 * @date 2020/12/31
*/

import * as program from 'commander';
import { version } from '../package.json';
import {Lz} from './command/lz';
import {LzCommit} from './command/lzCommit';
import './utils/consoleColor';
import {ConfigInquirer} from './inquirer/configInquirer';
import {LzPush} from './command/lzPush';
import {LzMr} from './command/lzMr';
import {LzConfig} from './command/lzConfig';
import {LzBranch} from './command/lzBranch';

let actionMap = {
    lz: {
        description: '从add开始一键化自动提交',
        usages: [
            'lzgit lz'
        ],
        alias: 'l',
        Class: Lz
    },
    commit: {
        description: '自动化提交',
        usages: [
            'lzgit commit'
        ],
        alias: 'c',
        Class: LzCommit
    },
    push: {
        description: '自动化推送',
        usages: [
            'lzgit push'
        ],
        alias: 'p',
        Class: LzPush
    },
    mr: {
        description: '自动化提交mr请求',
        usages: [
            'lzgit mr'
        ],
        alias: 'm',
        Class: LzMr
    },
    config: {
        description: '修改记录的配置参数',
        usages: [
            'lzgit config'
        ],
        alias: 'cf',
        Class: LzConfig
    },
    branch: {
        description: '新建工作分支',
        usages: [
            'lzgit branch'
        ],
        alias: 'b',
        Class: LzBranch
    }
};

Object.keys(actionMap).forEach((action) => {
    program
        .command(action)
        .description(actionMap[action].description)
        .alias(actionMap[action].alias)
        .action(async () => {
            await ConfigInquirer.checkConfig();

            // TODO 判断是否在git项目下

            await new actionMap[action].Class().done();
        });
});

program
    .version(version, '-v --version')
    .parse(process.argv);

if (!process.argv.slice(2).length) {
    program.outputHelp();
}
